//
//  BuyViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/27.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BuyViewController.h"
#import "BuyTableviewCell.h"
#import "BuyModel.h"
#import "BuyShopViewController.h"
@interface BuyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)UITableView *tableview;//tableview
@property (nonatomic, retain)NSMutableArray *dataArr;//tableview数据数组
@property (nonatomic, retain)UIImageView *headerimg;//头部图片
@property (nonatomic, retain)UILabel *headertitle;//头部标题
@property (nonatomic, retain)UILabel *headerContent;//头部内容

@end

@implementation BuyViewController

-(void)dealloc
{
    [_tableview release];
    [_dataArr release];
    [_headerContent release];
    [_headerimg release];
    [_headertitle release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//背景颜色
        //数据
    [self data];
    //创建tableview
    self.tableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //代理人
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    //添加
    [self.view addSubview:self.tableview];
    //重用池
    [_tableview registerClass:[BuyTableviewCell class] forCellReuseIdentifier:@"cell"];
    //释放
    [_tableview release];
    [self creat];

}
#pragma mark - tableview必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuyTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    BuyModel *buy = _dataArr[indexPath.row];
    cell.titlelabel.text = buy.title;//标题
    cell.content.text = buy.descriptioncontent;//内容
    [cell.img sd_setImageWithURL:[NSURL URLWithString:buy.pic]];//图片
    cell.i = buy.pic_height;//图片高度
    cell.j = buy.pic_width;//图片宽度
    return cell;
}
#pragma mark - 数据
- (void)data
{
    
    
    NSString *str = @"http://m.shougongke.com/index.php?c=Shiji&a=topicInfo&topic_id=64&vid=15";
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"][@"goods"];
        self.dataArr = [BuyModel modelHandlerWithArray:arr];

        NSDictionary *dic =  result[@"data"];
        [self.headerimg sd_setImageWithURL:[NSURL URLWithString:dic[@"host_pic" ]]];
        self.headertitle.text = dic[@"topic_name"];
        self.headerContent.text = dic[@"topic_des"];
        [_tableview reloadData];
    }];
}
#pragma mark - tableview行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuyModel *buy = _dataArr[indexPath.row];
    CGFloat h1 = [BuyTableviewCell heightWihtText:buy.descriptioncontent];
    CGFloat newh = [BuyTableviewCell heightWithPhoto:buy.pic_width height:buy.pic_height];
    if (buy.pic.length == 0 ) {
        return 30 + h1;
    }else
    {
        return h1  + newh + 30;
    }
}

#pragma mark - tableview的头部
- (void)creat
{
    //头部View
    UIView *bgview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    [self.tableview addSubview:bgview];
    self.tableview.tableHeaderView = bgview;//view作为tableview的头部
    //头部图片
    self.headerimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    [bgview addSubview:self.headerimg];
    //头部标题
    self.headertitle = [[UILabel alloc]initWithFrame:CGRectMake(5, 50, [UIScreen mainScreen].bounds.size.width - 10, 30)];
    self.headertitle.textColor = [UIColor whiteColor];//字体颜色
    self.headertitle.textAlignment = NSTextAlignmentCenter;//居中
    [self.headerimg addSubview:self.headertitle];
    //内容
    self.headerContent = [[UILabel alloc]initWithFrame:CGRectMake(0, 180, [UIScreen mainScreen].bounds.size.width, 150)];
    self.headerContent.font = [UIFont systemFontOfSize:14];//字体大小
    self.headerContent.numberOfLines = 0;//不限制行数
    [bgview addSubview:self.headerContent];
    
    
    [bgview release];
    [_headertitle release];
    [_headerimg release];
    [_headerContent release];
    
}
#pragma mark - tableView点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuyShopViewController *buy = [[BuyShopViewController alloc]init];
      BuyModel *b = _dataArr[indexPath.row];
    buy.str = b.tbk_url;//网址
    [self.navigationController pushViewController:buy animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
